package ru.aslanov.schedule.client;

import com.smartgwt.client.types.Side;
import com.smartgwt.client.types.TabBarControls;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.form.fields.events.ChangedEvent;
import com.smartgwt.client.widgets.form.fields.events.ChangedHandler;
import com.smartgwt.client.widgets.layout.HLayout;
import com.smartgwt.client.widgets.tab.Tab;
import com.smartgwt.client.widgets.tab.TabSet;
import ru.aslanov.schedule.client.ds.*;
import ru.aslanov.schedule.client.i18n.MyConstants;

import static ru.aslanov.schedule.client.i18n.I18nUtil.cnt;

/**
 * Created by IntelliJ IDEA.
 * Created: Feb 11, 2010 5:17:17 PM
 *
 * @author Sergey Aslanov
 */
public class SchedulePanel extends TabSet {
    private ScheduleUser scheduleUser;
    private GroupsPanel groupsPanel;
    private Tab groupsTab;

    public SchedulePanel(final ScheduleUser scheduleUser) {
        this.scheduleUser = scheduleUser;
        final TabSet tabSet = this;
        tabSet.setTabBarPosition(Side.TOP);

        tabSet.setTabBarThickness(25);
        tabSet.setTabBarAlign(Side.LEFT);

        tabSet.setWidth100();
        tabSet.setHeight100();

        initTabs(scheduleUser);

        final SelectItem langsSelect = new SelectItem("enterLang");
        langsSelect.setTitle(cnt().inputLanguage());
        langsSelect.setOptionDataSource(InputLangsDS.getInstance(scheduleUser));
        langsSelect.setValueField("id");
        langsSelect.setDisplayField("name");
        langsSelect.addChangedHandler(new ChangedHandler() {
            @Override
            public void onChanged(ChangedEvent event) {
                final String lang = langsSelect.getSelectedRecord().getAttribute("id");
                scheduleUser.setInputLang(lang);

                final int selectedTabNumber = tabSet.getSelectedTabNumber();
                final Tab[] tabs = tabSet.getTabs();
                for (Tab tab : tabs) {
                    Canvas pane = tab.getPane();
                    if (pane instanceof Invalidatable) {
                        ((Invalidatable)pane).invalidateData();
                    }
                    //tabSet.removeTab(tab);
                }
                //initTabs(scheduleUser);
                //tabSet.selectTab(selectedTabNumber);
            }
        });
        langsSelect.setWrapTitle(false);
        langsSelect.setDefaultValue(scheduleUser.getInputLang());
        DynamicForm dynamicForm = new DynamicForm();
        dynamicForm.setFields(langsSelect);

        Button printButton = new Button("");
        printButton.setIcon("[SKIN]/actions/print.png");
        printButton.setAutoFit(true);
        printButton.addClickHandler(new ClickHandler() {
            @Override
            public void onClick(ClickEvent event) {
                final Canvas pane = tabSet.getSelectedTab().getPane();
                if (pane instanceof Printable) {
                    Canvas.showPrintPreview(((Printable)pane).getPrintCanvas());
                } else {
                    Canvas.showPrintPreview(pane);
                }
            }
        });

        HLayout controls = new HLayout(5);
        controls.addMember(dynamicForm);
        controls.addMember(printButton);

        tabSet.setTabBarControls(TabBarControls.TAB_SCROLLER, TabBarControls.TAB_PICKER, controls);
    }

    private void initTabs(ScheduleUser scheduleUser) {
        final MyConstants cnt = cnt();
        TabSet tabSet = this;

        groupsPanel = new GroupsPanel(scheduleUser);
        groupsTab = new Tab(cnt.groups());
        groupsTab.setPane(groupsPanel);
        tabSet.addTab(groupsTab);

        final CalendarViewPanel calendarViewPanel = new CalendarViewPanel(scheduleUser, this);
        Tab calendarViewTab = new Tab(cnt.calendar());
        calendarViewTab.setPane(calendarViewPanel);
        tabSet.addTab(calendarViewTab);

        final DataPanel teachersPanel = new DataPanel(TeachersDS.getInstance(scheduleUser), cnt().teachers(), "100%", "100%", scheduleUser.getScheduleKey(), scheduleUser.isScheduleAdmin());
        Tab teachersTab = new Tab(cnt.teachers());
        teachersTab.setPane(teachersPanel);
        tabSet.addTab(teachersTab);

        final DataPanel locationsPanel = new DataPanel(LocationsDS.getInstance(scheduleUser), cnt().locations(), "100%", "100%", scheduleUser.getScheduleKey(), scheduleUser.isScheduleAdmin());
        Tab locationsTab = new Tab(cnt.locations());
        locationsTab.setPane(locationsPanel);
        tabSet.addTab(locationsTab);

        final DataPanel dancesPanel = new DataPanel(DancesDS.getInstance(scheduleUser), cnt().dances(), "100%", "100%", scheduleUser.getScheduleKey(), scheduleUser.isScheduleAdmin());
        Tab dancesTab = new Tab(cnt.dances());
        dancesTab.setPane(dancesPanel);
        tabSet.addTab(dancesTab);

        final DataPanel levelsPanel = new DataPanel(LevelsDS.getInstance(scheduleUser), cnt().levels(), "100%", "100%", scheduleUser.getScheduleKey(), scheduleUser.isScheduleAdmin());
        Tab levelsTab = new Tab(cnt.levels());
        levelsTab.setPane(levelsPanel);
        tabSet.addTab(levelsTab);

        if (scheduleUser.isScheduleAdmin()) {
            DataPanel usersPanel = new DataPanel(UserDS.getInstance(scheduleUser), cnt().users(), "100%", "100%", scheduleUser.getScheduleKey(), scheduleUser.isScheduleAdmin());
            Tab usersTab = new Tab(cnt.users());
            usersTab.setPane(usersPanel);
            tabSet.addTab(usersTab);

            InputLangsPanel langsPanel = new InputLangsPanel(scheduleUser);
            Tab langsTab = new Tab(cnt.inputLanguage());
            langsTab.setPane(langsPanel);
            tabSet.addTab(langsTab);

            GCalendarsPanel gCalendarsPanel = new GCalendarsPanel(groupsPanel, scheduleUser);
            Tab calendarsTab = new Tab(cnt.gcalendars());
            calendarsTab.setPane(gCalendarsPanel);
            tabSet.addTab(calendarsTab);

            SchedulePropertiesPanel schedulePropertiesPanel = new SchedulePropertiesPanel(scheduleUser);
            Tab schedulePropertiesTab = new Tab(cnt.scheduleProperties());
            schedulePropertiesTab.setPane(schedulePropertiesPanel);
            tabSet.addTab(schedulePropertiesTab);
        }
    }

    public String getScheduleKey() {
        return scheduleUser.getScheduleKey();
    }

    public void editGroup(String groupKey) {
        this.selectTab(groupsTab);
        groupsPanel.editGroup(groupKey);
    }
}
